<?php
/**
 * Created by PhpStorm.
 * User: asdf
 * Date: 2021/9/21
 * Time: 14:35
 */

namespace core\lib;


use function core\p;

class Queryinfom
{

    /**
     * @var 实例
     */
    private static $instance = null;

    /**
     * @var ip
     */
    public  $ip;

    /**
     * @var 请求头
     */
    public  $host;
    public  $port;
    public  $head;
    public  $referer;
    public  $useragent;
    public  $cookie;
    public  $session;
    public  $method;

    /**
     * @var 其他信息
     */
    public  $req_time;

    /**
     * @var 请求信息
     */
    public  $uri;
    public  $basuri;
    public  $uriarr = [];
    public  $url;
    public  $get    = [];
    public  $post   = [];
    public  $put    = [];
    public  $server = [];
    public  $file   = [];
    public  $domain;
    public  $produ;
    public  $selfs;

    /**
     * @MAPP 是否多应用
     */
    public $is_MAPP;



    private function __construct()
    {
        $this->Query();
    }

    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    public static function Newinstance(){
        if(!self::$instance instanceof self){
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function Query(){
        if (isset($_SERVER['REQUEST_URI'])){
            $this->uri = $_SERVER['REQUEST_URI'];
            $this->is_MAPP = strpos($this->uri,"MAPP")>0?true:false;
            $this->basuri = $this->is_MAPP?
                $this->MAPPurl():$this->Baseurl();
            $this->url = $_SERVER[''];
            $this->ip = $_SERVER["REMOTE_ADDR"];
            $this->port = $_SERVER["REMOTE_PORT"];

            $this->host = $_SERVER["REMOTE_HOST"];//host字段
            $this->method = $_SERVER["REQUEST_METHOD"];
            $this->req_time = $_SERVER["REQUEST_TIME"];
            $this->cookie = $_COOKIE;
            $this->session = $_SESSION;
            $this->head = $_SERVER;
            $this->referer = $this->head["HTTP_REFERER"];//referer 字段
            $this->file = $_FILES;
            $this->post = $_POST;
            $this->domain = $_SERVER["HTTP_HOST"];
            $this->produ = $_SERVER["SERVER_PROTOCOL"];
            $this->url = $_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
            $this->selfs = $_SERVER["PHP_SELF"];
            $this->uriarr = explode('/',trim($this->basuri,'/'));

            $this->MappGetparms();
            $this->Getparms();
            $this->Postparms();


        }
    }

    //基础url
    public function Baseurl(){
        $temp = explode("/".ENTRANCE,$this->uri);
        return $temp[1];
    }

    //MAPP url
    public function MAPPurl(){
        $temp = explode("/".ENTRANCE."/"."MAPP",$this->uri);
        return $temp[1];
    }

    public function Scriptself(){
        return $this->selfs;
    }

    public function root(){

    }

    //获取个体参数列表
    public function Getparms(){
        if ($this->is_MAPP){
            return null;
        }
        $count = count($this->uriarr);
        $par = [];
        for ($i = 2;$i < $count;$i+=2){
            if(isset($this->uri[$i+1])){

                $par[$this->uriarr[$i]] = $this->uriarr[$i+1];
            }
        }
        $this->get = $par;
        return $this->get;
    }

    //多应用参数获取
    public function MappGetparms(){
        if (!$this->is_MAPP){
            return null;
        }
        $count = count($this->uriarr);
        $par = [];
        for ($i = 3;$i < $count;$i+=2){
            if(isset($this->uri[$i+1])){

                $par[$this->uriarr[$i]] = $this->uriarr[$i+1];
            }
        }
        $this->get = $par;
        return $this->get;
    }

    //获取一个get参数
    public function Getaparm($name){
        return $this->get[$name];
    }

    //post列表
    public function Postparms(){
        return $this->post;
    }

    //获取一个post参数
    public function Postaparm($name){
        return $this->post[$name];
    }

    //请求方法
    public function Method(){
        return $this->method;
    }

    //是否是Get请求
    public function isGet(){
        return strtolower($this->method) == "get";
    }

    //是否是Post请求
    public function isPost(){
        return strtolower($this->method) == "post";
    }

    //是否是Head请求
    public function isHead(){
        return strtolower($this->method) == "head";
    }

    //是否是Put请求
    public function isPut(){
        return strtolower($this->method) == "put";
    }

    //是否是文件
    public function isFile(){
        return $this->isPost() && isset($_FILES) && !empty($_FILES);
    }

    public function Fileparms(){
        return $this->file;
    }



}